<?php

require_once LIBRARIES_ENTITIES_DATABASE;
require_once LIBRARIES_ENTITIES_CATEGORIES;
require_once LIBRARIES_ENTITIES_LOGO;
require_once LIBRARIES_ENTITIES_USERS;


require_once LIBRARIES_TOOLS_UTILITY;
require_once LIBRARIES_TOOLS_XTEMPLATE;
require_once LIBRARIES_TOOLS_PAGERBACKEND;

class LogoController {

    public function invoke() {

        $groupFunctions = $_SESSION['groupFunctions'];

        $userID = $_SESSION["session"]["sUserID"];


        $view = '';
        if (isset($_GET['view'])) {
            $view = $_GET['view'];
        }


        $action = '';
        if (isset($_POST['hdAction'])) {
            $action = $_POST['hdAction'];
        }


        $dir = dirname(__FILE__);

        $task = '';
        if (isset($_GET['task'])) {
            $task = $_GET['task'];
        }


        $rowToView = 10;
        if (isset($_POST["limit"])) {
            $rowToView = $_POST["limit"];
        }


        if ($view == 'logo') {

            switch ($action) {
                case 'publish':
                    $values = $_POST["cid"];
                    $result = $this->updateLogoStatus($values, true);
                    Utility::redirectCurrentPage();
                    break;
                case 'unpublish':
                    $values = $_POST["cid"];
                    $result = $this->updateLogoStatus($values, false);
                    Utility::redirectCurrentPage();
                    break;
                case 'orderdown':
                    $ID = $_POST["hdID"];
                    $result = $this->orderLogo($ID, 'down');
                    Utility::redirectCurrentPage();
                    break;
                case 'orderup':
                    $ID = $_POST["hdID"];
                    $result = $this->orderLogo($ID, 'up');
                    Utility::redirectCurrentPage();
                    break;
                case 'remove':
                    $values = $_POST["cid"];
                    $result = $this->deleteLogo($values);
                    if ($result == OK) {
                        Utility::messageBox('Xóa thành công');
                        Utility::redirectCurrentPage();
                    }
                    break;
                case 'add':
                case 'addAndExit':

                    $title = $_POST['txtTitle'];
                    $categoryID = $_POST['slCategoryID'];
                    $link = $_POST['txtLink'];

                    $indefinitely = 0;
                    if (isset($_POST['cbIndefinitely'])) {
                        $indefinitely = 1;
                    }
                    if ($indefinitely) {
                        $enableDate = '000-00-00';
                        $disableDate = '000-00-00';
                    } else {
                        $enableDate = Utility::formatDateTime($_POST['txtEnableDate'], $_POST['slEnableHour'], $_POST['slEnableMinute']);
                        $disableDate = Utility::formatDateTime($_POST['txtDisableDate'], $_POST['slDisableHour'], $_POST['slDisableMinute']);
                    }

                    $createdDate = Utility::formatDateTime($_POST['txtCreatedDate']);
                    $status = $_POST['rdoStatus'];
                    $path = $_POST['hdImage'];
                    $createdUserID = $userID;

                    $result = $this->addLogo($title, $categoryID, $link, $enableDate, $disableDate, $createdDate, $status, $path, $createdUserID, $indefinitely);

                    if ($result == OK) {
                        if ($action == 'add') {
                            Utility::messageBox('Thêm thành công');
                            Utility::redirectCurrentPage();
                        } else {
                            Utility::redirect(".?com=com_logo&view=logo&task=view&categoryID=" . $categoryID . "");
                        }
                    }
                    break;
                case 'edit':
                case 'editAndExit':

                    $ID = $_POST['hdID'];
                    $title = $_POST['txtTitle'];
                    $categoryID = $_POST['slCategoryID'];
                    $link = $_POST['txtLink'];

                    $indefinitely = 0;
                    if (isset($_POST['cbIndefinitely'])) {
                        $indefinitely = 1;
                    }
                    if ($indefinitely) {
                        $enableDate = '000-00-00';
                        $disableDate = '000-00-00';
                    } else {
                        $enableDate = Utility::formatDateTime($_POST['txtEnableDate'], $_POST['slEnableHour'], $_POST['slEnableMinute']);
                        $disableDate = Utility::formatDateTime($_POST['txtDisableDate'], $_POST['slDisableHour'], $_POST['slDisableMinute']);
                    }

                    $createdDate = Utility::formatDateTime($_POST['txtCreatedDate']);
                    $status = $_POST['rdoStatus'];
                    $path = $_POST['hdImage'];
                    $createdUserID = $userID;

                    $result = $this->updateLogo($title, $categoryID, $link, $enableDate, $disableDate, $createdDate, $status, $path, $createdUserID, $indefinitely, $ID);

                    if ($result == OK) {
                        if ($action == 'edit') {
                            Utility::messageBox('Cập nhật thành công');
                            Utility::redirectCurrentPage();
                        } else {
                            Utility::redirect(".?com=com_logo&view=logo&task=view&categoryID=" . $categoryID . "");
                        }
                    }
                    break;
            }





            if ($task == 'view') {

                if (Utility::checkPermission('view', $groupFunctions, LOGO_MANAGEMENT)) {

                    $viewFile = $dir . DS . 'views' . DS . 'logo' . DS . 'logo_list.html';
                    $xtemplate = new XTemplate($viewFile);

                    // category to filter
                    $categoryID = 0;
                    if (isset($_POST['slCategoryID'])) {
                        $categoryID = $_POST['slCategoryID'];
                    } else if (isset($_GET['categoryID'])) {
                        $categoryID = $_GET['categoryID'];
                    }


                    // CHECK PERMISSION
                    // check add button
                    if (Utility::checkPermission('add', $groupFunctions, LOGO_MANAGEMENT)) {
                        $xtemplate->assign('SELECTED_CATEGORY_ID', $categoryID);
                        $xtemplate->parse('main.add_button');
                    }
                    // check delete button
                    if (Utility::checkPermission('delete', $groupFunctions, LOGO_MANAGEMENT)) {
                        $xtemplate->parse('main.delete_button');
                    }
                    // check enable button
                    if (Utility::checkPermission('enable', $groupFunctions, LOGO_MANAGEMENT)) {
                        $xtemplate->parse('main.enable_button');
                    }
                    // check disable button
                    if (Utility::checkPermission('disable', $groupFunctions, LOGO_MANAGEMENT)) {
                        $xtemplate->parse('main.disable_button');
                    }


                    // check logo category button
                    if (Utility::checkPermission('disable', $groupFunctions, LOGO_CATEGORY_MANAGEMENT)) {
                        $xtemplate->parse('main.logo_category_button');
                    }



                    // index of checkbox
                    $indexCheckBox = 1;

                    // current page
                    $rurrentPage = 1;
                    if (isset($_POST["page"])) {
                        $rurrentPage = $_POST["page"];
                    }

                    // index of item
                    $index = 1;



                    $category = $this->getCategoryByID($categoryID);
                    if ($category != null) {
                        $xtemplate->assign('TITLE_CATEGORY', '[' . $category->getTitle() . ']');
                    }

                    $this->fillSelectBox_LogoCategory($xtemplate, $categoryID);

                    $logos = $this->getLogoListByCategoryID($categoryID);


                    // get pager object
                    $pager = PagerBackEnd::getPagerData(count($logos), $rowToView, $rurrentPage);

                    if ($logos != null) {

                        $indexCheckBox = 1;

                        foreach ($logos as $logo) {

                            if ($index > $pager->offset && $index <= $pager->limit) {

                                $xtemplate->assign('INDEX', $index);
                                $xtemplate->assign('INDEX_CHECKBOX', $indexCheckBox);
                                $xtemplate->assign('ID', $logo->getID());
                                $xtemplate->assign('PATH', $logo->getPath());
                                $xtemplate->assign('TITLE', $logo->getTitle());
                                $xtemplate->assign('LINK', $logo->getLink());

                                $xtemplate->assign('ENABLE_DATE', Utility::formatDateTimeToView($logo->getEnableDate(), true));
                                $xtemplate->assign('DISABLE_DATE', Utility::formatDateTimeToView($logo->getDisableDate(), true));


                                $xtemplate->assign('CREATED_USER', $logo->getCreatedUser()->getFullname());
                                $xtemplate->assign('MODIFIED_USER', $logo->getModifiedUser()->getFullname());
                                $xtemplate->assign('MODIFIED_DATE', Utility::formatDateTimeToView($logo->getModifiedDate()));
                                $xtemplate->assign('ORDER', $logo->getOrder());

                                // status
                                if ($logo->getStatus() == 1) {
                                    $xtemplate->assign('STATUS_ACTION', 'unpublish');
                                    $xtemplate->assign('STATUS_IMAGE', 'publish_g.png');
                                } else {
                                    $xtemplate->assign('STATUS_ACTION', 'publish');
                                    $xtemplate->assign('STATUS_IMAGE', 'publish_r.png');
                                }

                                // indefinitely
                                if ($logo->getIndefinitely() == 1) {
                                    $xtemplate->assign('INDEFINITELY_IMAGE', 'publish_g.png');
                                } else {
                                    $xtemplate->assign('INDEFINITELY_IMAGE', 'publish_r.png');
                                }

                                // check update permission                            
                                if (Utility::checkPermission('update', $groupFunctions, IMAGE_MANAGEMENT)) {
                                    $xtemplate->parse('main.row.update_button');
                                } else {
                                    $xtemplate->parse('main.row.update_button_disable');
                                }


                                // order
                                // if the the first order then show the down arrow
                                if ($index == 1) {
                                    $xtemplate->parse('main.row.down_arrow');
                                }
                                // if the last order the show the up arrow
                                else if ($index == count($logos)) {
                                    $xtemplate->parse('main.row.up_arrow');
                                }
                                // else show both arrow
                                else {

                                    $xtemplate->parse('main.row.up_down_arrow');
                                }


                                $xtemplate->parse('main.row');

                                $indexCheckBox++;
                            }
                            $index++;
                        }
                    }

                    // limit list
                    $xtemplate->assign('LIMIT_LIST', Utility::getLimitList($rowToView));

                    // show amount of row to view
                    $xtemplate->assign('ROW_TO_VIEW', $rowToView);

                    // show current page
                    $xtemplate->assign('CURRENT_PAGE', $rurrentPage);

                    // amount of item
                    $xtemplate->assign('AMOUNT_OF_ITEM', $pager->numPages);

                    // paging html
                    $xtemplate->assign('PAGING_HTML', $pager->paging);


                    $xtemplate->parse('main');
                    $xtemplate->out('main');
                } else {
                    echo ERROR_PERMISSION;
                }
            } else if ($task == 'add') {

                if (Utility::checkPermission('add', $groupFunctions, LOGO_MANAGEMENT)) {

                    $xtemplate = new XTemplate($dir . DS . 'views' . DS . 'logo' . DS . 'logo_add.html');

                    $categoryID = 0;
                    if (isset($_POST['slCategoryID'])) {
                        $categoryID = $_POST['slCategoryID'];
                    } else if (isset($_GET['categoryID'])) {
                        $categoryID = $_GET['categoryID'];
                    }

                    $this->fillSelectBox_LogoCategory($xtemplate, $categoryID);

                    $this->fillSelectBox_Hour($xtemplate, 'main.enable_hour');
                    $this->fillSelectBox_Hour($xtemplate, 'main.disable_hour');

                    $this->fillSelectBox_Minute($xtemplate, 'main.enable_minute');
                    $this->fillSelectBox_Minute($xtemplate, 'main.disable_minute');


                    $xtemplate->assign('TODAY', date('d/m/Y'));
                    $xtemplate->assign('UPLOAD_FOLDER', UPLOAD_FOLDER);
                    $xtemplate->assign('CATEGORY_ID', $categoryID);
                    $xtemplate->parse('main');
                    $xtemplate->out('main');
                } else {
                    echo ERROR_PERMISSION;
                }
            }
            // edit task
            else if ($task == 'edit') {

                if (Utility::checkPermission('update', $groupFunctions, LOGO_MANAGEMENT)) {

                    $ID = $_GET['ID'];

                    $logo = $this->getLogoByID($ID);

                    if ($logo != null) {

                        $xtemplate = new XTemplate($dir . DS . 'views' . DS . 'logo' . DS . 'logo_edit.html');

                        $this->fillSelectBox_LogoCategory($xtemplate, $logo->getCategoryID());

                        $xtemplate->assign('ID', $logo->getID());

                        $xtemplate->assign('LOGO_TITLE', $logo->getTitle());

                        $xtemplate->assign('PATH', $logo->getPath());
                        $xtemplate->assign('LINK', $logo->getLink());
                        $xtemplate->assign('MODIFIED_DATE', Utility::formatDateTimeToView($logo->getModifiedDate()));
                        $xtemplate->assign('CATEGORY_ID', $logo->getCategoryID());


                        $disableHour = substr($logo->getDisableDate(), 11, 2);
                        $disableMinute = substr($logo->getDisableDate(), 14, 2);
                        $enableHour = substr($logo->getEnableDate(), 11, 2);
                        $enableMinute = substr($logo->getEnableDate(), 14, 2);


                        $this->fillSelectBox_Hour($xtemplate, 'main.enable_hour', $enableHour);
                        $this->fillSelectBox_Hour($xtemplate, 'main.disable_hour', $disableHour);

                        $this->fillSelectBox_Minute($xtemplate, 'main.enable_minute', $enableMinute);
                        $this->fillSelectBox_Minute($xtemplate, 'main.disable_minute', $disableMinute);



                        // indefinitely
                        if ($logo->getIndefinitely() == 1) {
                            $xtemplate->parse('main.indefinitely_true');
                            $xtemplate->parse('main.hide_time');
                            $xtemplate->assign('ENABLE_DATE', Utility::formatDateTimeToView(date('d/m/Y')));
                            $xtemplate->assign('DISABLE_DATE', Utility::formatDateTimeToView(date('d/m/Y')));
                        } else {
                            $xtemplate->parse('main.indefinitely_false');
                            $xtemplate->assign('ENABLE_DATE', Utility::formatDateTimeToView($logo->getEnableDate()));
                            $xtemplate->assign('DISABLE_DATE', Utility::formatDateTimeToView($logo->getDisableDate()));
                        }


                        // status
                        if ($logo->getStatus() == 1) {
                            $xtemplate->parse('main.status_true');
                        } else {
                            $xtemplate->parse('main.status_false');
                        }
                        $xtemplate->parse('main');
                        $xtemplate->out('main');
                    } else {
                        echo 'Không tồn tại dữ liệu';
                    }
                } else {
                    echo ERROR_PERMISSION;
                }
            }
        }
        // view category
        else if ($view == 'category') {

            switch ($action) {
                case 'publish':
                    $values = $_POST["cid"];
                    $result = $this->updateCategoryStatus($values, true);
                    Utility::redirectCurrentPage();
                    break;
                case 'unpublish':
                    $values = $_POST["cid"];
                    $result = $this->updateCategoryStatus($values, false);
                    Utility::redirectCurrentPage();
                    break;
                case 'orderdown':
                    $categoryID = $_POST["hdCategoryID"];
                    $result = $this->orderCategory($categoryID, 'down');
                    Utility::redirectCurrentPage();
                    break;
                case 'orderup':
                    $categoryID = $_POST["hdCategoryID"];
                    $result = $this->orderCategory($categoryID, 'up');
                    Utility::redirectCurrentPage();
                    break;
                case 'remove':
                    $values = $_POST["cid"];
                    $result = $this->deleteCategory($values);
                    if ($result == OK) {
                        Utility::messageBox('Xóa thành công');
                        Utility::redirectCurrentPage();
                    }
                    break;
                case 'add':
                case'addAndExit':
                    $title = $_POST['txtTitle'];
                    $alias = Utility::removeSigns($_POST['txtAlias']);
                    $description = Utility::replaceHtmlSpecialChars($_POST['txtDescription']);
                    $parent = 0;
                    $createdDate = Utility::formatDateTime($_POST['createdDate']);
                    $status = $_POST['rdoState'];
                    $image = $_POST['hdImage'];

                    $createdUserID = $userID;

                    $result = $this->addCategory($parent, $title, $alias, $createdDate, $description, $createdUserID, $status, $image);

                    if ($result == OK) {
                        if ($action == 'add') {
                            Utility::messageBox('Thêm thành công');
                            Utility::redirectCurrentPage();
                        } else {

                            Utility::redirect(".?com=com_logo&view=category&task=view");
                        }
                    }
                    break;
                    
                case 'edit':
                case 'editAndExit':
                    $categoryID = $_POST['hdCategoryID'];
                    $title = $_POST['txtTitle'];
                    $alias = Utility::removeSigns($_POST['txtAlias']);
                    $description = Utility::replaceHtmlSpecialChars($_POST['txtDescription']);
                    $parent = 0;
                    $createdDate = Utility::formatDateTime($_POST['createdDate']);
                    $status = $_POST['rdoState'];
                    $image = $_POST['hdImage'];
                    $createdUserID = $userID;

                    $result = $this->updateCategory($parent, $title, $alias, $createdDate, $description, $createdUserID, $status, $categoryID, $image);

                    if ($result == OK) {
                        if ($action == 'edit') {
                            Utility::messageBox('Sửa thành công');
                            Utility::redirectCurrentPage();
                        } else {

                            Utility::redirect(".?com=com_logo&view=category&task=view");
                        }
                    }
                    break;
            }



            if ($task == 'view') {

                if (Utility::checkPermission('view', $groupFunctions, LOGO_CATEGORY_MANAGEMENT)) {
                    $xtemplate = new XTemplate($dir . '/views/category/category_list.html');

                    // CHECK PERMISSION
                    // check add button
                    if (Utility::checkPermission('add', $groupFunctions, LOGO_CATEGORY_MANAGEMENT)) {
                        $xtemplate->parse('main.add_button');
                    }
                    // check delete button
                    if (Utility::checkPermission('delete', $groupFunctions, LOGO_CATEGORY_MANAGEMENT)) {
                        $xtemplate->parse('main.delete_button');
                    }
                    // check enable button
                    if (Utility::checkPermission('enable', $groupFunctions, LOGO_CATEGORY_MANAGEMENT)) {
                        $xtemplate->parse('main.enable_button');
                    }
                    // check disable button
                    if (Utility::checkPermission('disable', $groupFunctions, LOGO_CATEGORY_MANAGEMENT)) {
                        $xtemplate->parse('main.disable_button');
                    }

                    $categoryID = 0;
                    if (isset($_POST['slCategoryID'])) {
                        $categoryID = $_POST['slCategoryID'];
                    } else if (isset($_GET['categoryID'])) {
                        $categoryID = $_GET['categoryID'];
                    }


                    $indexCheckBox = 1;


                    $rurrentPage = 1;
                    if (isset($_POST["page"])) {
                        $rurrentPage = $_POST["page"];
                    }

                    $index = 1;

                    $categories = $this->getCategoryListByComponentID(LOGO_COMPONENT);
                    $pager = PagerBackEnd::getPagerData(count($categories), $rowToView, $rurrentPage);
                    if ($categories != null) {
                        $indexCheckBox = 1;
                        for ($i = 0; $i < count($categories); $i++) {

                            if ($index > $pager->offset && $index <= $pager->limit) {

                                $xtemplate->assign('INDEX', $index);
                                $xtemplate->assign('INDEX_CHECKBOX', $indexCheckBox);
                                $xtemplate->assign('CATEGORY_ID', $categories[$i]->getID());
                                $xtemplate->assign('CATEGORY_TITLE', $categories[$i]->getTitle());
                                $xtemplate->assign('CATEGORY_ORDER', $categories[$i]->getOrder());
                                $xtemplate->assign('CREATED_USER', $categories[$i]->getCreateUser()->getFullname());
                                $xtemplate->assign('MODIFIED_USER', $categories[$i]->getModifiedUser()->getFullname());
                                $xtemplate->assign('CREATED_DATE', Utility::formatDateTimeToView($categories[$i]->getCreatedDate()));
                                $xtemplate->assign('MODIFIED_DATE', Utility::formatDateTimeToView($categories[$i]->getModifiedDate()));
                                $xtemplate->assign('IMAGE', $categories[$i]->getImage());

                                // check update permission                            
                                if (Utility::checkPermission('update', $groupFunctions, LOGO_CATEGORY_MANAGEMENT)) {
                                    $xtemplate->parse('main.category_list.update_button');
                                } else {
                                    $xtemplate->parse('main.category_list.update_button_disable');
                                }

                                // check menulist view permission                            
                                if (Utility::checkPermission('view', $groupFunctions, LOGO_MANAGEMENT)) {
                                    $xtemplate->parse('main.category_list.logolist_button');
                                } else {
                                    $xtemplate->parse('main.category_list.menulist_button_disable');
                                }

                                if ($categories[$i]->getState() == 1) {
                                    $xtemplate->assign('STATUS_ACTION', 'unpublish');
                                    $xtemplate->assign('STATUS_IMAGE', 'publish_g.png');
                                } else {
                                    $xtemplate->assign('STATUS_ACTION', 'publish');
                                    $xtemplate->assign('STATUS_IMAGE', 'publish_r.png');
                                }

                                if ($index == 1) {
                                    $xtemplate->parse('main.category_list.down_arrow');
                                } else if ($index == count($categories)) {
                                    $xtemplate->parse('main.category_list.up_arrow');
                                } else {
                                    $xtemplate->parse('main.category_list.up_down_arrow');
                                }

                                $xtemplate->parse('main.category_list');

                                $indexCheckBox++;
                            }
                            $index++;
                        }


                        $xtemplate->assign('LIMIT_LIST', Utility::getLimitList($rowToView));


                        $xtemplate->assign('ROW_TO_VIEW', $rowToView);

                        $xtemplate->assign('CURRENT_PAGE', $rurrentPage);


                        $xtemplate->assign('AMOUNT_OF_PAGE', $pager->numPages);


                        $xtemplate->assign('PAGING_HTML', $pager->paging);
                    }
                    $xtemplate->parse('main');
                    $xtemplate->out('main');
                } else {
                    echo ERROR_PERMISSION;
                }
            } else if ($task == 'add') {
                if (Utility::checkPermission('add', $groupFunctions, LOGO_CATEGORY_MANAGEMENT)) {
                    $xtemplate = new XTemplate($dir . '/views/category/category_add.html');

                    $xtemplate->assign('TODAY', date('d/m/Y'));
                    $xtemplate->parse('main');
                    $xtemplate->out('main');
                } else {
                    echo ERROR_PERMISSION;
                }
            } else if ($task == 'edit') {
                if (Utility::checkPermission('update', $groupFunctions, LOGO_CATEGORY_MANAGEMENT)) {
                    $categoryID = '';
                    if (isset($_GET['categoryID'])) {
                        $categoryID = $_GET['categoryID'];
                    }

                    $category = $this->getCategoryByID($categoryID);

                    if ($category != null) {

                        $xtemplate = new XTemplate($dir . DS . 'views' . DS . 'category' . DS . 'category_edit.html');

                        $xtemplate->assign('CATEGORY_MODIFIED_DATE', $category->getModifiedDate());

                        $xtemplate->assign('CATEGORY_TITLE', $category->getTitle());


                        $xtemplate->assign('CATEGORY_ALIAS', $category->getAlias());

                        $xtemplate->assign('CATEGORY_DESCRIPTION', $category->getDescription());

                        $xtemplate->assign('CATEGORY_ID', $category->getID());

                        $xtemplate->assign('IMAGE', $category->getImage());

                        if ($category->getState() == 1) {
                            $xtemplate->parse('main.state_true');
                        } else {
                            $xtemplate->parse('main.state_false');
                        }
                        $xtemplate->parse('main');
                        $xtemplate->out('main');
                    } else {
                        Utility::redirect('.?com = com_logo&view=category&task=view');
                    }
                } else {
                    echo ERROR_PERMISSION;
                }
            }
        } else {
            Utility::redirect('.');
        }
    }

    private function getLogoListByCategoryID($categoryID) {
        $db = new Database();
        try {
            $logo = new Logo();
            $logo->setCategoryID($categoryID);
            $result = $logo->getListByCategoryID($db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }

    private function addLogo($title, $categoryID, $link, $enableDate, $disableDate, $createdDate, $status, $path, $createdUserID, $indefinitely) {
        $db = new Database();
        try {

            $logo = new Logo();
            $logo->setTitle($title);
            $logo->setCategoryID($categoryID);
            $logo->setLink($link);
            $logo->setIndefinitely($indefinitely);
            $logo->setEnableDate($enableDate);
            $logo->setDisableDate($disableDate);

            $logo->setStatus($status);
            $logo->setPath($path);
            $logo->setCreatedDate($createdDate);
            $logo->setModifiedDate($createdDate);

            $logo->setCreatedUserID($createdUserID);
            $logo->setModifiedUserID($createdUserID);
            $logo->setOrder($logo->getMaxOrder($db));

            $logo->add($db);

            $db->submitChanges();

            return OK;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            return false;
        }
    }

    private function updateLogo($title, $categoryID, $link, $enableDate, $disableDate, $createdDate, $status, $path, $createdUserID, $indefinitely, $ID) {
        $db = new Database();
        try {
            $logo = new Logo();
            $logo->setID($ID);
            $logo->setTitle($title);
            $logo->setCategoryID($categoryID);
            $logo->setLink($link);
            $logo->setIndefinitely($indefinitely);
            $logo->setEnableDate($enableDate);
            $logo->setDisableDate($disableDate);

            $logo->setStatus($status);
            $logo->setPath($path);
            $logo->setModifiedDate($createdDate);
            $logo->setModifiedUserID($createdUserID);
            $logo->setOrder($logo->getMaxOrder($db));

            $logo->update($db);

            $db->submitChanges();

            return OK;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            return false;
        }
    }

    private function getLogoByID($ID) {
        $db = new Database();
        try {
            $logo = new Logo();
            $logo->setID($ID);
            $result = $logo->getByID($db);

            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            return null;
        }
    }

    // hàm cập nhật highlight bài viết
    private function deleteLogo($values) {
        $db = new Database();
        try {
            for ($i = 0; $i < count($values); $i++) {
                $logo = new Logo();
                $logo->setID($values[$i]);
                $logo->delete($db);
            }
            $db->submitChanges();
            return OK;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }

    // hàm cập nhật trạng thái bài viết
    private function updateLogoStatus($values, $status) {
        $db = new Database();
        try {
            for ($i = 0; $i < count($values); $i++) {
                $logo = new Logo();
                $logo->setID($values[$i]);
                if ($status == true) {
                    $logo->setStatus(1);
                } else {
                    $logo->setStatus(0);
                }
                $logo->updateStatus($db);
            }
            $db->submitChanges();
            return OK;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }

    // order item
    private function orderLogo($ID, $action) {
        $db = new Database();
        try {
            $logo = new Logo();
            $logo->setID($ID);
            if ($action == 'up') {
                $logo->orderUp($db);
            } else {
                $logo->orderDown($db);
            }
            $db->submitChanges();
            return OK;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }

    private function getCategoryListByComponentID($componentID) {
        $db = new Database();
        try {
            $category = new Categories();
            $category->setComponentID($componentID);
            $result = $category->getListByComponentID($db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->submitChanges();
            exit();
        }
    }

    private function fillSelectBox_Hour($xtemplate, $block, $selected = '') {
        for ($i = 0; $i < 24; $i++) {
            if ($selected != '') {
                if ($i == $selected) {
                    $xtemplate->assign('SELECTED', 'selected');
                } else {
                    $xtemplate->assign('SELECTED', '');
                }
            }
            $xtemplate->assign('VALUE', $i);
            $xtemplate->assign('TITLE', $i);
            $xtemplate->parse($block);
        }
    }

    private function fillSelectBox_Minute($xtemplate, $block, $selected = '') {
        for ($i = 0; $i < 60; $i++) {

            if ($selected != '') {
                if ($i == $selected) {
                    $xtemplate->assign('SELECTED', 'selected');
                } else {
                    $xtemplate->assign('SELECTED', '');
                }
            }
            $xtemplate->assign('VALUE', $i);
            $xtemplate->assign('TITLE', $i);
            $xtemplate->parse($block);
        }
    }

    // show category list
    private function fillSelectBox_LogoCategory($xtemplate, $selectedID = null) {

        $categories = $this->getCategoryListByComponentID(LOGO_COMPONENT);

        foreach ($categories as $category) {

            $xtemplate->assign('CATEGORY_ID', $category->getID());
            $xtemplate->assign('CATEGORY_TITLE', $category->getTitle());


            if ($selectedID != null) {
                if ($selectedID == $category->getID()) {
                    $xtemplate->assign('SELECTED_CATEGORY', 'selected');
                } else {
                    $xtemplate->assign('SELECTED_CATEGORY', '');
                }
            } else {
                if (isset($_POST['slCategoryID'])) {

                    if ($_POST['slCategoryID'] == $category->getID()) {
                        $xtemplate->assign('SELECTED_CATEGORY', 'selected');
                    } else {
                        $xtemplate->assign('SELECTED_CATEGORY', '');
                    }
                } else if (isset($_GET['categoryID'])) {

                    if ($_GET['categoryID'] == $category->getID()) {
                        $xtemplate->assign('SELECTED_CATEGORY', 'selected');
                    } else {
                        $xtemplate->assign('SELECTED_CATEGORY', '');
                    }
                } else {
                    $xtemplate->assign('SELECTED_CATEGORY', '');
                }
            }
            $xtemplate->parse('main.category_list');
        }
    }

    // update category status
    private function updateCategoryStatus($values, $status) {

        try {
            $db = new Database();
            for ($i = 0; $i < count($values); $i++) {
                $category = new Categories();
                $category->setID($values[$i]);
                if ($status == true) {
                    $category->setState(1);
                } else {
                    $category->setState(0);
                }
                $category->updateState($db);
            }
            $db->submitChanges();
            return OK;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            exit();
        }
    }

    // hàm tải chi tiết 1 category
    private function getCategoryByID($categoryID) {
        try {
            $db = new Database();
            $category = new Categories();
            $category->setID($categoryID);
            $category = $category->getByID($db);
            $db->submitChanges();
            return $category;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            exit();
        }
    }

    // delete a category
    private function deleteCategory($values) {
        try {
            $db = new Database();
            for ($i = 0; $i < count($values); $i++) {
                $category = new Categories();
                $category->setID($values[$i]);
                $category->delete($db);
            }
            $db->submitChanges();
            return OK;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            exit();
        }
    }

    // order category
    private function orderCategory($categoryID, $action) {
        $db = new Database();
        try {
            $category = new Categories();
            $category->setID($categoryID);
            $categoryLevel = $category->getByID($db);
            $category->setComponentID(LOGO_COMPONENT);
            $category->setLevel($categoryLevel->getLevel());

            if ($action == 'up') {
                $category->orderUp($db);
            } else {
                $category->orderDown($db);
            }
            $db->submitChanges();
            return OK;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }

    // hàm thêm 1 bài viết
    private function addCategory($parent, $title, $alias, $createdDate, $description, $createdUserID, $status, $image) {
        $db = new Database();
        try {

            $category = new Categories();

            // id cha
            $category->setParent($parent);

            // tieu de
            $category->setTitle($title);
            // alias
            $category->setAlias($alias);

            // ngay tao
            $category->setCreatedDate($createdDate);
            // ngay cap nhat
            $category->setModifiedDate($createdDate);

            // nguoi tao
            $category->setCreatedUserID($createdUserID);
            // nguoi cap nhat
            $category->setModifiedUserID($createdUserID);

            // mo ta
            $category->setDescription($description);
            // trang thai
            $category->setState($status);

            $category->setImage($image);

            // cap bac
            $category->setLevel($category->getNextLevelByParentID($db));

            // ma thanh phan
            $category->setComponentID(LOGO_COMPONENT);

            // thu tu
            $category->setOrder($category->getMaxOrder($db));

            // them category
            $result = $category->add($db);

            $db->submitChanges();
            if ($result > 0) {
                return OK;
            } else {
                
            }
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }

    // hàm cập nhật 1 bài viết
    private function updateCategory($parent, $title, $alias, $createdDate, $description, $createdUserID, $status, $ID, $image) {
        $db = new Database();
        try {

            $category = new Categories();

            // id cha
            $category->setID($ID);

            // id cha
            $category->setParent($parent);

            // tieu de
            $category->setTitle($title);

            // alias
            $category->setAlias($alias);

            // ngay cap nhat
            $category->setModifiedDate($createdDate);

            // nguoi cap nhat
            $category->setModifiedUserID($createdUserID);

            // mo ta
            $category->setDescription($description);

            // trang thai
            $category->setState($status);

            $category->setImage($image);

            // cap bac
            $category->setLevel($category->getNextLevelByParentID($db));


            // update category
            $category->update($db);

            $db->submitChanges();
            return OK;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }

}

?>
